package cn.itsource.ymcc.consumer;

import cn.itsource.ymcc.constant.Constants;
import cn.itsource.ymcc.constant.ConstantsOrder;
import cn.itsource.ymcc.constant.ConstantsOrderMq;
import cn.itsource.ymcc.domain.CourseOrder;
import cn.itsource.ymcc.service.ICourseOrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

@Slf4j
@Component
//consumerGroup = 定义组
//topic = 主题
//selectorExpression = tag
@RocketMQMessageListener(
        consumerGroup = "OrderOutTimeConsumer",
        topic = ConstantsOrderMq.TOPIC_COURSE_ORDER_DEALY,
        selectorExpression = ConstantsOrderMq.TAGS_COURSE_ORDER_DEALY
)
public class OrderOutTimeConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private ICourseOrderService service;
    @Override
    public void onMessage(MessageExt messageExt) {
        try {
            //得到消息
            byte[] body = messageExt.getBody();
            //转为字符串  -- json字符串
            String orderNo = new String(body, Constants.UTF8);
            CourseOrder courseOrder = service.getOne(new QueryWrapper<CourseOrder>().eq("order_no", orderNo));
            if(courseOrder.getStatusOrder() == ConstantsOrder.ORDER_STATUS_0){
                courseOrder.setStatusOrder(ConstantsOrder.ORDER_STATUS_4);
                service.updateById(courseOrder);
            }
            log.info("订单超时修改成功");
        } catch (UnsupportedEncodingException e) {
            log.info("订单超时修改失败");
            e.printStackTrace();
        }
    }
}
